﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;
using static WHControlLib.Controls.WHLine;

namespace WHControlLib.Controls
{
    public partial class WHLine : UserControl
    {
        public WHLine()
        {
            //设置双缓冲开启禁止背景擦拭
            SetStyle(
                     ControlStyles.OptimizedDoubleBuffer
                     | ControlStyles.ResizeRedraw
                     | ControlStyles.Selectable
                     | ControlStyles.AllPaintingInWmPaint
                     | ControlStyles.UserPaint
                     | ControlStyles.SupportsTransparentBackColor,
                     true);

            InitializeComponent();
        }

            Rectangle  MyRect=new Rectangle();  


        private Color _lineFirstColor=Color.Blue;
        [Category("A我的"), Description("该控件第一种的颜色，默认，蓝色"), Browsable(true)]
        public Color LineFirstColor
        {
            get { return _lineFirstColor; }
            set { _lineFirstColor = value; Invalidate(); }
        }

        private Color _lineSecondColor = Color.Yellow;
        [Category("A我的"), Description("该控件可启用的第二种颜色，默认，黄色"), Browsable(true)]
        public Color LineSecondColor
        {
            get { return _lineSecondColor; }
            set { _lineSecondColor = value; Invalidate(); }
        }

         private Color _lineThirdColor = Color.Red;
        [Category("A我的"), Description("该控件可启用的第三种颜色，默认，红色"), Browsable(true)]
    
        public Color LineThirdColor 
        {
            get { return _lineThirdColor; }
            set { _lineThirdColor = value; Invalidate(); }
        }

     public   enum UseColorAmount
        {
            one,two,three
        }

        private UseColorAmount _myUseColorAmount=UseColorAmount.one;
        [Category("A我的"), Description("该控件启用的颜色数量，默认，一种"), Browsable(true)]
        public UseColorAmount MyUseColorAmount
        {
            get { return _myUseColorAmount; }
            set { _myUseColorAmount = value; Invalidate(); }
        }
        private int lineSize = 2;

        [Category("A我的"), Description("该控件线条宽度"), Browsable(true)]
    
        [DefaultValue(1)]
        public int LineSize
        {
            get => lineSize;
            set
            {
                lineSize = Math.Max(1, value);
                Invalidate();
            }
        }
        private WHLineCap startCap = WHLineCap.None;

        [DefaultValue(WHLineCap.None), Category("A我的")]
        public WHLineCap StartCap
        {
            get => startCap;
            set
            {
                startCap = value;
                Invalidate();
            }
        }

        private WHLineCap endCap = WHLineCap.None;

        [DefaultValue(WHLineCap.None), Category("A我的")]
        public WHLineCap EndCap
        {
            get => endCap;
            set
            {
                endCap = value;
                Invalidate();
            }
        }

        private int lineCapSize = 6;

        [DefaultValue(6), Category("A我的")]
        public int LineCapSize
        {
            get => lineCapSize;
            set
            {
                lineCapSize = value;
                Invalidate();
            }
        }

        public enum WHLineCap
        {//Flat = 0,
            //Square = 1,
            //Round = 2,
            //Triangle = 3,
            //NoAnchor = 0x10,
            //SquareAnchor = 17,
            //RoundAnchor = 18,
            //DiamondAnchor = 19,
            //ArrowAnchor = 20,
            //Custom = 0xFF,
            //AnchorMask = 240




            /// <summary>
            /// 无
            /// </summary>
            None,

            /// <summary>
            /// 正方形
            /// </summary>
            Square,

            /// <summary>
            /// 菱形
            /// </summary>
            Diamond=19,

            /// <summary>
            /// 三角形
            /// </summary>
            Triangle=3,

            /// <summary>
            /// 圆形
            /// </summary>
            Circle=2
        }

        /// <summary>
        /// 线型
        /// </summary>
        public enum WHLineDashStyle
        {
            




            /// <summary>
            /// 实线
            /// </summary>
            Solid,

            /// <summary>
            /// 虚线
            /// </summary>
            Dash,

            /// <summary>
            /// 由重复的点图案构成的直线
            /// </summary>
            Dot,

            /// <summary>
            /// 由重复的点划线图案构成的直线
            /// </summary>
            DashDot,

            /// <summary>
            /// 由重复的双点划线图案构成的直线
            /// </summary>
            DashDotDot,

            /// <summary>
            /// 自定义
            /// </summary>
            Custom,

        
        }

        WHLineDashStyle lineDashStyle = WHLineDashStyle.Solid;
        [Description("线的样式"), Category("A我的")]
        [DefaultValue(WHLineDashStyle.Solid)]
        public WHLineDashStyle LineDashStyle
        {
            get => lineDashStyle;
            set
            {
                if (lineDashStyle != value)
                {
                    lineDashStyle = value;
                    Invalidate();
                }
            }
        }



  


        public enum LineDec
        {
            Horizontal, vertical
        }
        private LineDec _myLineDec=LineDec.Horizontal;
        [Category("A我的"), Description("线控件启用的方向，默认，水平"), Browsable(true)]
        public LineDec MyLineDec
        {
            get { return _myLineDec; }
            set { _myLineDec = value; Invalidate(); }
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);

            MyRect = e.ClipRectangle;

            Graphics Myg = e.Graphics;
            Myg.SmoothingMode = SmoothingMode.AntiAlias;
            Myg.CompositingQuality = CompositingQuality.HighQuality;
            Myg.InterpolationMode = InterpolationMode.HighQualityBicubic;
            DrawLIneDeffentColor(Myg, MyRect, MyLineDec);


        }

        private void DrawLIneDeffentColor(Graphics Myg,Rectangle myrect,LineDec mylineDec)
        {
            Point startP, endP;
            switch (MyUseColorAmount)
            {
                case UseColorAmount.one:
           using (SolidBrush sb = new SolidBrush(LineFirstColor))
                  {   
                      using (Pen p=new Pen(sb,LineSize))
                        {
                            //Point startP, endP;
                            p.DashStyle = (DashStyle)((int)LineDashStyle);
                            p.StartCap = (LineCap)((int)startCap);
                            p.EndCap = (LineCap)((int)endCap);
                        

                            DrawDecLine(mylineDec,  out startP, out endP);

                            Myg.DrawLine(p, startP, endP);
                        }
                    }
                    break;
                case UseColorAmount.two:
                   
                    LinearGradientMode linegraMode;
                    if (mylineDec==LineDec.Horizontal)
                    {
                        linegraMode = LinearGradientMode.Horizontal;

                    }
                    else
                        linegraMode = LinearGradientMode.Vertical;

                    using (LinearGradientBrush lineBrush = new LinearGradientBrush(myrect, LineFirstColor, LineSecondColor, linegraMode))
                    {
                        using (Pen p = new Pen(lineBrush, LineSize))
                        {
                            p.DashStyle = (DashStyle)((int)LineDashStyle);
                            p.StartCap = (LineCap)((int)startCap);
                            p.EndCap = (LineCap)((int)endCap);



                            //Point startP, endP;
                           DrawDecLine(mylineDec,  out startP, out endP);

                            Myg.DrawLine(p, startP, endP);
                        }
                    }


                    break;
                case UseColorAmount.three:
                    //Color[] colorarrary = new Color[] { LineFirstColor, LineSecondColor, LineThirdColor };

                    //ColorBlend colorBlend = new ColorBlend();
                    //colorBlend.Colors = colorarrary;
                    //colorBlend.Positions = new float[] { 0f, 0.5f, 1.0f };
                    //using (LinearGradientBrush superLinearbrush = new LinearGradientBrush(myrect, Color.Transparent, Color.Transparent, LinearGradientMode.Horizontal))
                    //{
                    //    using (Pen p = new Pen(superLinearbrush, LineSize))
                    //    {
                    //        Point startP, endP;
                    //        drawDecLine(mylineDec, out startP, out endP);

                    //        Myg.DrawLine(p, startP, endP);
                    //    }
                    //}

                    using (SolidBrush sb = new SolidBrush(LineFirstColor))
                    {
                        using (Pen p = new Pen(sb, LineSize))
                        {
                            //Point startP, endP;
                            p.DashStyle = (DashStyle)((int)LineDashStyle);
                            p.StartCap = (LineCap)((int)startCap);
                            p.EndCap = (LineCap)((int)endCap);


                            DrawDecLine(mylineDec, out startP, out endP);

                            Myg.DrawLine(p, startP, endP);
                        }
                    }

                    break;
                default:
                    break;
            }
        }

        private void DrawDecLine(LineDec mylineDec,  out Point startP, out Point endP)
        {
            int Startlinecapsize = (int)LineSize / 2 + 1;
             int Endlinecapsize= (int)LineSize / 2 + 1;
            if (startCap==WHLineCap.None)
            {
                Startlinecapsize = 0;

            }
            if (endCap==WHLineCap.None)
            {
                Endlinecapsize = 0;
            }



            if (mylineDec == LineDec.Horizontal)
            {
                startP = new Point(0+ Startlinecapsize, (int)(Height ) / 2);
                endP = new Point(Width- Endlinecapsize, (int)(Height ) / 2);

            }
            else
            {
                startP = new Point((int)(Width ) / 2, +Startlinecapsize);
                endP = new Point((int)(Width ) / 2, Height - Endlinecapsize);

            }
        }


        /////////////////////////////////////////////////////////

    }
}
